import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * Computer Network, Programming Assignment #2
 * 
 * Send Server Class
 * @author Yancheng Li
 *
 */

public class SendServer extends Thread{
	private Point pos;
	private int port;
	private PGPeer peer;
	private ServerSocket welcomeSocket = null;
	
	SendServer(Point pos, PGPeer peer, int port){
		this.pos = pos;
		this.port = port;
		this.peer = peer;
	}
	
	
	
	public void shutdown(){
		if(welcomeSocket != null)
			try {
				welcomeSocket.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	}
	
	public void run(){
		try{
			
			PSPUtil.print("Send server " + pos + "start...listening on port " + port, true);
			welcomeSocket = new ServerSocket(port);			
			while(true) {
				
				Socket connectionSocket = welcomeSocket.accept();
				PSPUtil.print("Send thread start...", true);
				
				SendThread thread = new SendThread(connectionSocket, peer, pos);				
				thread.start();
				Thread.sleep(100);
				
		   }
		}catch(Exception e){
			System.out.println(e.getMessage());
		}
	}
	
}
